﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;

namespace School.Common
{
    public static class Gradding
    {
        private static SchoolEntities db = new SchoolEntities();

        public static IEnumerable<GradePolicy> GetGradePolicy(long grade)
        {
            List<GradePolicy> rows = new List<GradePolicy>();

            var subjects = from x in db.TE_Subject where x.Grad_Id == grade select x;

            var currentPolicy = from x in db.TE_Grade_Success_Policy where x.Grad_Id == grade select x;

            foreach (var x in subjects)
            {
                byte type;

                try
                {
                    type = currentPolicy.Where(y => y.Subject_Id == x.Id).Select(y => y.Type).First();
                }
                catch
                {
                    type = 1;
                }

                rows.Add(new GradePolicy() { SubjectId = x.Id, SubjectName = x.Subject_Name, Type = type });
            }

            return rows;
        }

        public static void SetupGradePolicy(long grade, IEnumerable<GradePolicy> rows)
        {
            var currentPolicy = from x in db.TE_Grade_Success_Policy where x.Grad_Id == grade select x;

            foreach (var x in currentPolicy)
            {
                db.DeleteObject(x);
            }

            db.SaveChanges();

            foreach (var x in rows)
            {
                db.AddToTE_Grade_Success_Policy(new TE_Grade_Success_Policy()
                {
                    Subject_Id = x.SubjectId,
                    Type = x.Type,
                    Grad_Id = grade
                });
            }

            db.SaveChanges();
        }

        public static IEnumerable<SubjectTotal> GetSubjectsTotal(long student, byte type)
        {
            List<SubjectTotal> st = new List<SubjectTotal>();

            long grade = (from x in db.TE_Student where x.Id == student select x.Grade_ID).First();

            grade = (from x in db.ORG_Grade_To_Grade where x.Grade_Id2 == grade select x.Grade_Id).First();

            var subjects = from x in db.TE_Grade_Success_Policy where x.Grad_Id == grade && x.Type == type select x;            

            foreach (var subject in subjects)
            {
                decimal subjectTotal = 0;

                var exams = from x in db.TE_Subject_Policy_Exam where x.TE_Subject_Success_policy.Sub_Id == subject.Subject_Id select x;

                try
                {
                    var subjectData = db.TE_Subject_Success_policy.Where(s => s.Sub_Id == subject.Subject_Id).First();

                    decimal minDegree = decimal.Parse(subjectData.Min_degree.ToString());
                    decimal maxDegree = decimal.Parse(subjectData.Max_degree.ToString());

                    foreach (var exam in exams)
                    {
                        var examSubject = (from x in db.TE_Offline_Exam_Subject where x.Exam_Id == exam.Exam_Id && x.Subject_Id == subject.Subject_Id select x).First();

                        decimal examMinDegree = examSubject.Min;
                        decimal examMaxDegree = examSubject.Max;

                        var studentDegree = (from x in db.TE_Offline_Exam_Degree where x.Exam_Id == exam.Exam_Id && x.Subject_Id == subject.Subject_Id && x.Student_Id == student select x).First();

                        decimal tempMaxPercentage = (exam.Parcent / 100) * maxDegree;
                        subjectTotal += studentDegree.Degree * tempMaxPercentage / examMaxDegree;
                    }

                    st.Add(new SubjectTotal()
                    {
                        SubjectName = subject.TE_Subject.Subject_Name,
                        SubjectId = subject.Subject_Id,
                        Max = maxDegree,
                        Min = minDegree,
                        StudentDegree = subjectTotal
                    });
                }
                catch
                {
                    continue;
                }                
            }

            return st;
        }
    }

    public class GradePolicy
    {
        public string SubjectName { get; set; }
        public int SubjectId { get; set; }
        public byte Type { get; set; }
    }

    public class SubjectTotal
    {
        public string SubjectName { get; set; }
        public int SubjectId { get; set; }
        public decimal Min { get; set; }
        public decimal Max { get; set; }
        public decimal StudentDegree { get; set; }
    }
}
